<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\data\CurrentShopData;
use App\Service\ShopConfigService;
use Hyperf\DbConnection\Db;
use Hyperf\Validation\Rule;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;
use Swoole\Exception;

/**
 * 店铺配置
 * Class ShopConfigController
 * @package App\Controller
 */
class ShopConfigController extends AbstractController
{

    /**
     * @Inject
     * @var ShopConfigService
     */
    private $shopConfig;

    /**
     * 获取登录用户信息
     * @return int
     */
    public function get_user_id(): int
    {
        $user_id = (int)$this->request->getHeaderLine('admin_id') ?? 0;
        if (empty($user_id)) {
            throw new BusinessException(ErrorCode::NORMAL_ERROR, '未登录，请先登录');
        }
        return $user_id;
    }

    /**
     * 设置配送配置(一次仅配置一种类型)
     * @return ResponseInterface
     */
    public function setDeliveryConfig(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        $data = $this->shopConfig->deliveryConfigValidator($user_id);
        return $this->jsonResponse->success(['config_id' => $this->shopConfig->setDeliveryConfig($data)]);
    }

    /**
     * 获取配送配置详情
     * @return ResponseInterface
     */
    public function getDeliveryConfig(): ResponseInterface
    {
        $user_id = $this->get_user_id();
        $type = (int)$this->request->input('type', 1);
        return $this->jsonResponse->success($this->shopConfig->getDeliveryConfig($user_id, $type));
    }

    /**
     * 获取商家配送配置
     * @return ResponseInterface
     */
    public function getDeliveryConfigs()
    {
        $user_id = $this->get_user_id();
        $status = (int)$this->request->input('status', 0);
        return $this->jsonResponse->success($this->shopConfig->getDeliveryConfigs($user_id, $status));
    }

    /**
     * 检测并开启第三方配送
     * @return ResponseInterface
     */
    public function setThirdConfig()
    {
        $user_id = (int)$this->getAdminId();
        $locate_point = (string)$this->request->input('locate_point', '');
        $lat = (float)$this->request->input('point_lat', 0);
        $lng = (float)$this->request->input('point_lng', 0);
        $act = (int)$this->request->input('act', 1);
        if (!$this->shopConfig->setThirdConfig($user_id, $locate_point, $lat, $lng, $act)) {
            return $this->jsonResponse->error(ErrorCode::NORMAL_ERROR, '该地区暂不支持配送');
        }
        return $this->jsonResponse->success([], 'ok!');
    }


    public function open_time_set()
    {
        $shop_info = CurrentShopData::getData();
        $request_p =  $this->request->inputs(['open_time']);
        $this->validation($request_p,
            [
                'open_time'=>[
                    "required",
                    "array",
                    "distinct",
                    "min:0",
                ],
                'open_time.*.start_time'=>[
                    "required",
                    "integer",
                    "distinct",
                    "min:0",
                    "max:2400",
                ],
                'open_time.*.end_time'=>[
                    "required",
                    "integer",
                    "distinct",
                    "min:0",
                    "max:2400",
                ],
            ],
            ['shop_id.unique'=>'商铺已经设置，请刷新查看进行修改']
        );
        Db::beginTransaction();
        try {
            $this->shopConfig->set_open_time(intval($shop_info->id),$request_p['open_time']);
            Db::commit();
            return $this->jsonResponse->success([], '设置成功!');
        }catch (Exception $e){
            Db::rollBack();
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID, $e->getMessage());
        }
    }
    public function open_time_get()
    {
        $shop_info = CurrentShopData::getData();
        $list = $this->shopConfig->get_open_time(intval($shop_info->id));
        return $this->jsonResponse->success($list, '设置详情!');
    }
    public function public_open_time_get()
    {
        $request_p =  $this->request->inputs(['shop_id']);
        $this->validation($request_p,
            [
                'shop_id'=>[
                    Rule::exists('fly_shop','id'),
                    "integer",
                    "min:1",
                ],
            ],
        );
        $list = $this->shopConfig->get_open_time(intval($request_p['shop_id']));
        return $this->jsonResponse->success($list, '设置详情!');
    }

}